package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.pojo.Vo.GetItemByProductIdVo;
import csu.web.mypetstore.pojo.Item;
import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.service.ProductService;
import csu.web.mypetstore.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 18632
 * @description 针对表【product】的数据库操作Service实现
 * @createDate 2025-03-10 16:27:13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService{
    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据产品 ID 获取单个商品信息
     */
    @Override
    public Product getProduct(String productId) {

        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);

        return productMapper.selectOne(wrapper);
    }

    /**
     * 根据分类 ID 获取该分类下的所有商品列表
     */
    @Override
    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.selectList(
                new LambdaQueryWrapper<Product>()
                        .eq(Product::getCategory, categoryId) // 查询条件：categoryId
        );
    }

    /**
     * 根据关键字搜索商品列表（模糊匹配商品名称或描述）
     */
    @Override
    public List<Product> searchProductList(String keyword) {
        return productMapper.selectList(
                new LambdaQueryWrapper<Product>()
                        .like(Product::getName, keyword)   // 商品名称匹配
                        .or()
                        .like(Product::getDescn, keyword)  // 商品描述匹配
        );
    }

    @Override
    public void addNewProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void deleteProduct(String productId) {
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.delete(wrapper);
    }

    @Override
    public void updateProductName(String name, String productId) {
        Product product = new Product();
        product.setName(name);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }


    @Override
    public void updateProductDescn(String descn, String productId) {
        Product product = new Product();
        product.setDescn(descn);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }

    @Override
    public void updateProductCategory(String category, String productId) {
        Product product = new Product();
        product.setCategory(category);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }
}




